package com.cyou.qb.admin.web;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.cyou.nad.tag.table.pagebar.PageBar;
import com.cyou.qb.common.service.ActivityService;
import com.cyou.qb.system.SpringMvcUtils;

public class BaseController {
	protected Log log =LogFactory.getLog(getClass());
	protected static int DEFAULT_PAGE_SIZE = 20;
	
	protected int getPageSize(int defaultValue){
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		String ret = request.getParameter(PageBar.REQUEST_PARAMETER_PAGE_SIZE);
		if(StringUtils.isBlank(ret)||!NumberUtils.isDigits(ret))
			return defaultValue;
		return Integer.parseInt(ret);
	}
	
	protected int getPageNo(){
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		String ret = request.getParameter(PageBar.REQUEST_PARAMETER_PAGE);
		if(StringUtils.isBlank(ret)||!NumberUtils.isDigits(ret))
			return 1;
		return Integer.parseInt(ret);
	}
	
	protected HttpServletRequest getRequest(){
		return SpringMvcUtils.getRequest();
	}

	
	protected HttpSession getSession(){
		return SpringMvcUtils.getSession();
	}
	
	@SuppressWarnings("rawtypes")
	protected void saveAllParameters(HttpServletRequest request) {
		String[] params;
		String key;
		for (Enumeration enu = request.getParameterNames(); enu.hasMoreElements();) {
			key = (String) enu.nextElement();
			params = request.getParameterValues(key);
			if (!(params == null || params.length < 1)) {
				request.setAttribute(key, (params.length > 1) ? params :StringUtils.trimToEmpty(params[0]));
			} 
		}
	}
	
	protected void print(HttpServletResponse response,String msg) throws IOException{
		response.setContentType("text/html; charset=UTF-8");
		response.getWriter().print(msg);
	}
	
	protected int getFaceStatus(HttpServletRequest request,Model model,ActivityService activityService,String name){
		String activeId =  StringUtils.trimToEmpty(request.getParameter(name)); 
		int actFaceStatus = activityService.findFaceStatus(activeId);
		Assert.hasText(activeId,"未获取活动id");
		Assert.isTrue(StringUtils.isNumeric(activeId),"活动id格式错误");
		model.addAttribute("actFaceStatus", actFaceStatus);
		model.addAttribute(name, activeId);
		return actFaceStatus;
	}
	
	public static void main(String[] args) {
		System.out.println(StringUtils.trimToNull(null));
		
	}
}
